package org.darkforest.common.services.impl;

import org.apache.commons.lang.StringUtils;
import org.darkforest.common.services.StoreExtendsService;
import org.darkforest.dao.mapper.DAddressMapper;
import org.darkforest.dao.mapper.ExpressMapper;
import org.darkforest.dao.mapper.StoreExtendMapper;
import org.darkforest.dao.mapper.StoreMapper;
import org.darkforest.common.services.DeliverService;
import org.darkforest.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liu on 15/1/18.
 */
@Service("deliverService")
public class DeliverServiceImpl implements DeliverService {
    @Autowired
    private DAddressMapper daddressMapper;
    @Autowired
    private ExpressMapper expressMapper;
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private StoreExtendsService storeExtendsService;

    @Override
    public List selectAddress(int shopId) {
        return daddressMapper.selectAddress(shopId);
    }
    public int insertAddress(DAddress address){
        return daddressMapper.insertSelective(address);
    }
    @Override
    public DAddress selectByPrimaryKey(int addressId){
        return daddressMapper.selectByPrimaryKey(addressId);
    }
    @Override
    public int updateAddress(DAddress address) {
        return daddressMapper.updateByPrimaryKeySelective(address);
    }
    public int deleteAddress(int addressId){
        return daddressMapper.deleteByPrimaryKey(addressId);
    }




    public List selectAllExpress(){
        return expressMapper.selectAllExpress();
    }
    public List selectAllUsedExpress(){
        return expressMapper.selectAllUsedExpress();
    }
    public Express selectByExpressId(int expressId){
        return expressMapper.selectByExpressId(expressId);
    }
    public void updateEOrders(List  list){
        expressMapper.updateEOrders(list);
    }
    public void  updateDefault(){
        expressMapper.updateDefault();
    }
    public Store getSoreByid(int id){
        return storeMapper.selectStoreByPrimaryKey(id);
    }
    public List selectAllStore(){
        return storeMapper.selectAllStore();
    }
    public int updateStore(StoreWithBLOBs store){
        return storeMapper.updateByPrimaryKeySelective(store);
    }
    public List selectAllOfExpress(List  list) {
        return expressMapper.selectAllOfExpress(list);
    }

    /**
     * 根据店铺编号获取店铺设置的快递
     * @param storeId
     * @return
     */
    @Override
    public List<Express> getStoreExpress(Integer storeId) {

        //String express = storeExtendsService.selectByPrimaryKey(storeId).getExpress();
        StoreExtend storeExtend=storeExtendsService.selectByPrimaryKey(storeId);
        String express = null;
        if(storeExtend != null && StringUtils.isNotEmpty(storeExtend.getExpress())) {
            express = storeExtend.getExpress();
        }
        List<Integer> expressIds = new ArrayList<Integer>();
        if(express!=null && StringUtils.isNotEmpty(express)){
            String[] eIds = express.split(",");
            if(eIds!=null && eIds.length>0){
                for(String id :eIds){
                    expressIds.add(Integer.parseInt(id));
                }
            }
        }
        return expressIds.size()==0 ? null : this.selectAllOfExpress(expressIds);
    }
}
